package com.ubaer.zsycz.ui.base.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.Window;

import com.lsw.shareUser.ShareUserInfoUtil;
import com.lsw.util.AppManager;
import com.lsw.util.Helper;
import com.lsw.util.HideSoftKeyboardUtil;
import com.lsw.util.StatusBarCompat;
import com.ubaer.zsycz.MyApplication;
import com.ubaer.zsycz.R;
import com.ubaer.zsycz.inject.components.ActivityComponent;
import com.ubaer.zsycz.inject.components.DaggerActivityComponent;
import com.ubaer.zsycz.inject.modules.ActivityModule;
import com.ubaer.zsycz.ui.activity.LoginActivity;
import com.ubaer.zsycz.ui.base.presenter.IPresenter;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * author：lsw
 */
public abstract class BaseActivity<T extends IPresenter> extends FragmentActivity {

    protected Activity mContext;
    @Inject
    protected T mPresenter;
    protected Unbinder unbinder;

    protected ActivityComponent mActivityComponent;

    //布局文件id
    protected abstract int getLayoutId();

    //注入
    protected abstract void initInjector();

    //初始化事件和数据
    protected abstract void initEventAndData();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        initBaseConfig();
        setContentView(getLayoutId());
        AppManager.getAppManager().addActivity(this);
        unbinder = ButterKnife.bind(this);
        Helper.statusBarLightMode(this);
        mActivityComponent = DaggerActivityComponent.builder().appComponent(MyApplication.getAppComponent())
                .activityModule(new ActivityModule(this)).build();
        initInjector();
        if (mPresenter != null) mPresenter.attachView(this, this);
        initEventAndData();
        // 点击EditText以外的区域或者控件隐藏软键盘
        HideSoftKeyboardUtil.setupUI(getContentView(), (Activity) mContext);
    }

    protected View getContentView() {
        return findViewById(android.R.id.content);
    }

    private void initBaseConfig() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        SetStatusBarColor();
    }

    /**
     * 着色状态栏（4.4以上系统有效）
     */
    protected void SetStatusBarColor() {
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorPrimary));
    }

    /**
     * 着色状态栏（4.4以上系统有效）
     */
    protected void SetStatusBarColor(int color) {
        StatusBarCompat.setStatusBarColor(this, color);
    }

    /**
     * 沉浸状态栏（4.4以上系统有效）
     */
    protected void SetTranslanteBar() {
        StatusBarCompat.translucentStatusBar(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
        unbinder.unbind();
        AppManager.getAppManager().finishActivity(this);
    }

    /**
     * 用户单点登录
     */
    public void OpenLogin() {
        ShareUserInfoUtil.getInstance(mContext).clearCache();
        Intent intent = new Intent(mContext, LoginActivity.class);
        startActivity(intent);
        finish();
        AppManager.getAppManager().finishAllActivity();
    }

    /**
     * 注销退到登录页
     *
     * @param isfinsh 是否关闭当前页
     */
    public void openLogin(boolean isfinsh) {
        Intent intent = new Intent(mContext, LoginActivity.class);
        startActivity(intent);
        if (isfinsh) {
            ((Activity) mContext).finish();
        }
    }

    //用intent启动拨打电话
    public void call(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        try {
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
